var Sources = (function(){
	var sources = {};
	var div;
	
	return {
		add: function(label, url, src, flashVars, width, height, opacity, reTitle, reValue, reChange){
		  sources[label] = {
		    "url": url,
		    embed: {
		      "src": src,
		      "flashVars": flashVars,
		      "width": width,
		      "height": height,
		      "opacity": opacity
		    },
		    regex: {
		      title: reTitle,
		      value: reValue,
		      change: reChange
		    }
		  };
		},
		
		get: function(label){
		  return sources[label];			
		},
		
		selection: function(source) {
		  var select = function() {
		    var anchors = this.div.getElementsByTagName("a");
		    for (var i = 0; i < anchors.length; i++) {
		      if (anchors[i] === this.e.srcElement) {
		        anchors[i].style.backgroundImage = anchors[i].style.backgroundImage.replace(/\_unsel/, "_sel");
		      } else {
		        anchors[i].style.backgroundImage = anchors[i].style.backgroundImage.replace(/\_sel/, "_unsel");
		      }
		    }
		    var matches = /image\/(.*?)\_(?:un)?sel/.exec(this.e.srcElement.style.backgroundImage);
		    this.div.getElementsByTagName("input")[0].value = matches[1];
		  };
		  var div = document.createElement("div");
		  div.className = "sources";
		
		  var hidden = document.createElement("input");
		  hidden.type = "hidden";
		  hidden.value = source;
		  div.appendChild(hidden);
		
		  for (var name in this.sources) {
		    var anchor = document.createElement("a");
		    anchor.href = "#";
		    anchor.style.backgroundImage = "url('/image/" + name + (source === name ? "_" : "_un") + "selected.png')";
		    anchor.onclick = function(e) { select.call({"div": div, "e": e}); return false; };
		    anchor.onkeydown = function(e) {
		      switch(e.which) {
		        case 13: //enter
		          e.preventDefault();
		          break;
		        case 32: // space bar
		          select.call({"div": div, "e": e});
		          break;
		        case 37: // left arrow
		          var prev = e.srcElement.previousSibling;
		          if (prev.tagName = "A") {
		            prev.focus();
		          }
		          break;
		        case 39: // right arrow
		          var next = e.srcElement.nextSibling;
		          if (next.tagName = "A") {
		            next.focus();
		          }
		          break;
		        default:
		          break;
		      }
		    };
		    div.appendChild(anchor);
		  }
		  return div;
		},

		graph: function(secid, src){
		  // Clear context node
		  if (this.div.hasChildNodes()) {
		    while (this.div.childNodes.length >= 1) {
		      this.div.removeChild(this.div.firstChild);
		    }
		  }
		
		  var source = sources.sources[src];
		  var params = source.embed;
		
		  var a = document.createElement("a");
		  var aText = document.createTextNode("Go to site");
		  a.href = source.url + secid;
		  a.target = "_blank";
		  a.appendChild(aText);
		
		  var br = document.createElement("br");
		  
		  var embed = "<embed flashvars='" + params.flashVars.replace("{secid}", secid) + "' allowscriptaccess=\"always\" " +
		    "bgcolor=\"#ffffff\" height=\"" + params.height + "px\" id=\"chartImg\" loop=\"false\" menu=\"false\" name=\"test\" " +
		    "pluginspage=\"http://www.macromedia.com/go/getflashplayer\" src=\"" + params.src + "\" " +
		    "type=\"application/x-shockwave-flash\" width=\"" + params.width + "px\" wmode=\"" + params.opacity + "\">";
		  this.div.innerHTML = embed;
		  this.div.insertBefore(br, this.div.firstChild);
		  this.div.insertBefore(a, br);
		}
	}
})();